package com.c.yinyuezhushou;

import androidx.annotation.MainThread;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.c.yinyuezhushou.Dialog.MyDialog;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.MyRetrofit;
import com.c.yinyuezhushou.MRetrofit.RetrofitService;
import com.c.yinyuezhushou.Sql.MyDButil;
import com.c.yinyuezhushou.Sql.MySql;
import com.c.yinyuezhushou.Ui.HomeActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    MyApplication myApplication;
    private TextView textView;
    private ImageView imageView;

    public static String ac="1.4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        MySql myHleper = new MySql(getApplicationContext(),"MusicItem.db",null,1);
        SQLiteDatabase sqLiteDatabase = myHleper.getReadableDatabase();
        MyDButil myDButil = new MyDButil((MyApplication) getApplication(),sqLiteDatabase);
        myApplication = (MyApplication) getApplicationContext();
        myApplication.setMyDButil(myDButil);

        imageView=findViewById(R.id.amain_image);
        imageView.setImageResource(R.drawable.icon_icon);
        textView=findViewById(R.id.amain_text);
        textView.setText("检查更新...");
        getDown();
    }

    public void getDown(){
        MyRetrofit.getCk(myApplication, RetrofitService.class).getUpdate(0).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    JSONObject jsonObject1 = new JSONObject(jsonObject.getString("data"));

                    if (jsonObject1.getString("ac").equals(ac)){
                        textView.setText("获取配置...");
                        getCk();
                    }else{
                        Log.d("test1","ss");
                        MyDialog myDialog = new MyDialog(MainActivity.this, false, null, "发现更新!!!",jsonObject1.getString("message"), new MyInterface() {
                            @Override
                            public void con() {
                                Intent intent = new Intent();
                                intent.setAction("android.intent.action.VIEW");
                                try {
                                    Uri uri = Uri.parse(jsonObject1.getString("down"));
                                    intent.setData(uri);
                                    startActivity(intent);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void can() throws JSONException {
                                if (jsonObject1.getBoolean("isCompulsory")){
                                    Toast.makeText(myApplication, "此次为强制更新", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(myApplication, "此次非强制更新", Toast.LENGTH_SHORT).show();
                                    textView.setText("获取配置...");
                                    getCk();
                                }

                            }

                            @Override
                            public void musicItem(MusicItem musicItem) {

                            }
                        });
                        myDialog.show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(myApplication, "连接服务器失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getCk(){
        MyRetrofit.getCk(MainActivity.this, RetrofitService.class).getCk().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                textView.setText("获取配置成功");
                myApplication.setCk(response.headers().values("Set-Cookie"));
                Intent  intent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(myApplication, "连接服务器失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getDown();
    }
}